package com.itheima.canal.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * creste by itheima.itcast
 */
@Configuration
public class RabbitMQConfig {

    //上架交换机名称
    public static final String GOODS_UP_EXCHANGE = "goods_up_exchange";
    //下架交换机名称
    public static final String GOODS_DOWN_EXCHANGE = "goods_down_exchange";
    //定义更新广告队列名称
    public static final String AD_UPDATE_QUEQU = "ad_update_quequ";
    //定义上架队列名称
    public static final String SEARCH_ADD_QUEUE = "search_add_queue";
    //定义下架队列名称
    public static final String SEARCH_DEL_QUEUE = "search_del_queue";
    //定义页面改变队列
    public static final String PAGE_CREATE_QUEUE="page_create_queue";
    @Bean(PAGE_CREATE_QUEUE)
    public Queue PAGE_CREATE_QUEUE(){
        return new Queue(PAGE_CREATE_QUEUE);
    }
    //声明广告队列
    @Bean(AD_UPDATE_QUEQU)
    public Queue queue() {
        return new Queue(AD_UPDATE_QUEQU);
    }
    //声明下架队列
    @Bean(SEARCH_DEL_QUEUE)
    public Queue SEARCH_DEL_QUEUE() {
        return new Queue(SEARCH_DEL_QUEUE);
    }
    //声明下架交换机
    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange GOODS_DOWN_EXCHANGE() {
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }
    //定义下架交换机和队列的绑定关系
    @Bean
    public Binding GOODS_DOWN_EXCHANGE_BINDING(
            @Qualifier(SEARCH_DEL_QUEUE) Queue queue,
            @Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
    //定义上架队列
    @Bean(SEARCH_ADD_QUEUE)
    public Queue search_add_queue() {
        return new Queue(SEARCH_ADD_QUEUE);
    }

    //定义上架交换机
    @Bean(GOODS_UP_EXCHANGE)
    public Exchange goods_up_exchange() {
        //发布订阅模式 fanout类型的交换机
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }

    //定义上架交换机和队列的绑定关系

    /**
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding goods_up_exchange_binding(
            @Qualifier(SEARCH_ADD_QUEUE) Queue queue,
            @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange) {
        //bind 队列
        //to   交换机
        //with routingKey
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();

    }
    //定义静态化页面和上架交换机的绑定
    @Bean
    public Binding PAGE_CREATE_QUEUE_BINDING(@Qualifier(PAGE_CREATE_QUEUE) Queue queue,
                                             @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }


}
